package org.whuims.pois.action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.whuims.pois.common.TimeUtils;
import org.whuims.pois.dao.NewsDao;
import org.whuims.pois.helper.ReportDataHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created by 声伟 on 2014/4/26 0026.
 */
public class ReportsTodayTimeSerialAction extends Action {
    private NewsDao newsDao = new NewsDao();

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String planguage = request.getParameter("planguage");
        String startDate = TimeUtils.getDay(null, TimeUtils.getNow(), -7, null);
//        String startDate = "2013-10-10";
        String endDate = TimeUtils.getNow();
//        String endDate = "2013-11-10";
        String type = request.getParameter("type");
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return null;
        }
        List<List<Calendar>> list = newsDao.timeserial(type, topicId, planguage, startDate, endDate);
        List<Map<String, Long>> result = ReportDataHelper.timedata(list); //list包含三个系列（未实现），map是不同时间的数量分布
//        GraphData graphData = graphData(result);
//        Gson gson = new Gson();
//        String data = gson.toJson(graphData);
        String data = ReportDataHelper.jqPlotData(result, false);
        request.setAttribute("ajaxResult", data);
        return null;
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }
}
